﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel.Composition;
using ModularAndPlugin.WinApp.Contracts;
using WeifenLuo.WinFormsUI.Docking;
using Menu = UI.WinForm.Menu;
namespace UI.WinForm
{
    [Export(typeof(IHost))]
    public partial class frmHost : Form, IHost
    {
        public frmHost()
        {
            InitializeComponent();
        }

        private void frmHost_Load(object sender, EventArgs e)
        {
            //Here, the exported menus will be attached to the main menu.
            //foreach (var menu in Program._modHandler.MenuList)
            //{
            //    this.menuStrip1.Items.Add(menu.WinFormsMenu);
            //    //var dd = menu.Value.WinFormsMenu;
            //}
            Menu menuObj = new Menu();
            menuObj.menuHandler += new Menu.menuEventHandler(ClickShowForm);
            menuObj.CreateMenu(this);
        }


        private void sairToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmHost_FormClosing(object sender, FormClosingEventArgs e)
        {
            Exit();
        }

        private static void Exit()
        {
            if (Program._modHandler != null)
            {
                //call the ModuleHandler Dispose method
                Program._modHandler.Dispose();
                Program._modHandler = null;
            }
            Application.Exit();
        }


        public void showForm(DockContentEx frm)
        {
            IDockContent dmt = FindDocument(frm.Text);
            if (dmt == null)
            {
                frm.MdiParent = this;

                frm.Show(dockPanel);
            }
            else
            {
                this.dockPanel.ActivePane.ActiveContent = dmt;
            }
        }

        /// <summary>
        /// 根据菜单名执行相应的方法
        /// </summary>
        /// <param name="vokeName">菜单描述</param>
        private void ClickShowForm(string vokeName)
        {
            Cursor cur = this.Cursor;
            try
            {
                this.Cursor = Cursors.WaitCursor;
                var cmd = Program._modHandler.FindCmdByKey(vokeName);
                if (cmd == null) return;


                cmd.DynamicInvoke(null);

            }
            catch (Exception ex)
            {

                MessageBox.Show("出现异常");
            }
            finally
            {
                this.Cursor = cur;
            }
        }
        private IDockContent FindDocument(string text)
        {
            if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
            {
                foreach (Form form in MdiChildren)
                    if (form.Text == text)
                        return form as IDockContent;

                return null;
            }
            else
            {
                foreach (IDockContent content in dockPanel.Documents)
                    if (content.DockHandler.TabText == text)
                        return content;

                return null;
            }
        }
    }
}
